﻿//SecurityChangeTypePicker.xaml.cs
//Copyright (c) 2013 StockSharp LLC, all rights reserved.
//This code module is part of StockSharp library.
//This code is licensed under the GNU GENERAL PUBLIC LICENSE Version 3.
//See the file License.txt for the license details.
//More info on: http://stocksharp.com

namespace StockSharp.Xaml
{
	using System.Collections.ObjectModel;
	using System.Linq;

	using Ecng.Common;
	using Ecng.ComponentModel;
	using Ecng.Collections;

	using StockSharp.Algo.Storages;

	/// <summary>
	/// Графическая панель для выбора <see cref="SecurityChangeTypes"/>.
	/// </summary>
	public partial class SecurityChangeTypePicker
	{
		private sealed class Item : NotifiableObject
		{
			public Item(SecurityChangeTypes type)
			{
				Type = type;
			}

			public SecurityChangeTypes Type { get; private set; }

			public string Name
			{
				get { return Type.GetDisplayName(); }
			}

			private bool _isSelected;

			public bool IsSelected
			{
				get { return _isSelected; }
				set
				{
					_isSelected = value;
					NotifyChanged("IsSelected");
				}
			}
		}

		private readonly ObservableCollection<Item> _items;

		/// <summary>
		/// Создать <see cref="SecurityChangeTypePicker"/>.
		/// </summary>
		public SecurityChangeTypePicker()
		{
			InitializeComponent();

			_items = new ObservableCollection<Item>(Enumerator.GetValues<SecurityChangeTypes>().Select(t => new Item(t)));
			ItemsSource = _items;
		}

		/// <summary>
		/// Выбранные <see cref="SecurityChangeTypes"/>.
		/// </summary>
		public SecurityChangeTypes ChangeTypes
		{
			get
			{
				return _items.Where(item => item.IsSelected).Aggregate(default(SecurityChangeTypes), (current, item) => current | item.Type);
			}
			set
			{
				var values = value.SplitMask();
				_items.ForEach(i => i.IsSelected = values.Contains(i.Type));
			}
		}
	}
}